define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/member/index',
                    add_url: 'member/member/add',
                    edit_url: 'member/member/edit',
                    del_url: 'member/member/del',
                    multi_url: 'member/member/multi',
                    table: 'member',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'openid', title: __('Openid'),operate:false},
                        {field: 'avatar_image', title: __('Avatar_image'), formatter: Table.api.formatter.image,operate:false},
                        {field: 'username', title: __('Username'),operate:'LIKE'},
                        {field: 'nickname', title: __('Nickname'),operate:'LIKE'},
                        {field: 'mobile', title: __('Mobile'),operate:'LIKE'},
                        {field: 'identity',title: __('Identity'), searchList: {"1":__('Identity 1'),"2":__('Identity 2'),"3":__('Identity 3'),"4":__('Identity 4'),"5":__('Identity 5')}, formatter: Table.api.formatter.normal},
                        {field: 'membersn', title: __('Membersn'),operate:false},
                        {field: 'partner.name', title: __('Partner.name'),operate:false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('查看'),
                            table: table,
                            events: Table.api.events.buttons,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('我的下线'),
                                    title: __('我的下线'),
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: 'member/member/offline'
                                },
                                {
                                    name: 'detail',
                                    text: __('佣金'),
                                    title: __('佣金'),
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: 'member/member/commission'
                                },
                            ],
                            operate:false,
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'agentid', title: __('上线')},
                        {field: 'credit', title: __('Credit'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        // {field: 'openid', title: __('Openid')},
                        // {field: 'avatar_image', title: __('Avatar_image'), formatter: Table.api.formatter.image},
                        // {field: 'username', title: __('Username')},
                        // {field: 'nickname', title: __('Nickname')},
                        // {field: 'mobile', title: __('Mobile')},
                        // {field: 'agentid', title: __('Agentid')},
                        // {field: 'identity', title: __('Identity'), searchList: {"1":__('Identity 1'),"2":__('Identity 2'),"3":__('Identity 3'),"4":__('Identity 4'),"5":__('Identity 5')}, formatter: Table.api.formatter.normal},
                        // {field: 'membersn', title: __('Membersn')},
                        // {field: 'p_id', title: __('P_id')},
                        // {field: 'credit', title: __('Credit'), operate:'BETWEEN'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'partner.id', title: __('Partner.id')},
                        // {field: 'partner.name', title: __('Partner.name')},
                        // {field: 'partner.createtime', title: __('Partner.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            var identity = Config.pid;
            if(identity == 1){
                $('.pid').show();
            }else{
                $('.pid').hide();
            }

            //选择身份
            $('#c-identity').change(function () {
                var selected = $('#c-identity option:selected').val()
                var id = $('#c-id').val();
                if(selected == 1){
                    $('.pid').show();
                    getMemberSn(id);
                }else{
                    $('.pid').hide();
                    $('#c-p_id option:selected').val('')
                }
            })

            function getMemberSn(id){
                $.ajax({
                    url:'/admin/member/member/getMemberSn',
                    method:'GET',
                    data:{'id':id},
                    success:function(res){
                        if(res.code == 1){
                            $('#c-membersn').val(res.data);
                        }
                    }
                })
            }
            Controller.api.bindevent();
        },
        //我的下线
        offline:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/member/offlineList/ids/' + Config.ids,
                    table: 'member',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'avatar_image', title: __('Avatar_image'), formatter: Table.api.formatter.image,operate:false},
                        {field: 'username', title: __('Username'),operate:'LIKE'},
                        {field: 'nickname', title: __('Nickname'),operate:'LIKE'},
                        {field: 'mobile', title: __('Mobile'),operate:'LIKE'},
                        {field: 'identity', title: __('Identity'), searchList: {"1":__('Identity 1'),"2":__('Identity 2'),"3":__('Identity 3'),"4":__('Identity 4'),"5":__('Identity 5')}, formatter: Table.api.formatter.normal},
                        {field: 'credit', title: __('Credit'), operate:false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('查看'),
                            table: table,
                            events: Table.api.events.buttons,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看订单'),
                                    title: __('查看订单'),
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: 'member/member/order'
                                },
                            ],
                            operate:false,
                            formatter: Table.api.formatter.buttons
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //下线的订单
        order:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/member/orderList/ids/' + Config.ids,
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'ordersn', title: __('Ordersn'),operate:'LIKE'},
                        {field: 'member.nickname', title: __('购买者昵称'),operate:'LIKE'},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'prepay', title: __('Prepay'), operate:false},
                        {field: 'residue', title: __('Residue'), operate:false},
                        {field: 'discount.title', title: __('Discount.title'),operate:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'),  operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //佣金
        commission:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/member/commissionList/ids/' + Config.ids,
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                showFooter:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'ordersn', title: __('Ordersn'),operate:'LIKE'},
                        {field: 'member.nickname', title: __('购买者昵称'),operate:'LIKE'},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'prepay', title: __('Prepay'), operate:false},
                        {field: 'residue', title: __('Residue'), operate:false},
                        {field: 'status', title: __('Status'), operate:false,searchList: {"0":__('Status 0'),"1":__('Status 1'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'),  operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ],
                ],
            });



            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});